home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 642 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.7 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: "Wil Evers" <wil@ittpub.nl>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: STL pop_back()
  5. Date: 6 Mar 1996 15:44:28 GMT
  6. Organization: ?
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <0099EED891E3C360.3D60AAB8@ittpub.nl>
  9. NNTP-Posting-Host: taumet.eng.sun.com
  10. Content-Type: text
  11. X-Vms-Mail-To: IN%"std-c++@ncar.ucar.edu"
  12. Content-Length: 2000
  13. X-Lines: 55
  14. Originator: clamage@taumet
  15.  
  16. In article <4hhsta$2gk@news.BelWue.DE>
  17. kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl) writes:
  18.  
  19. > However, 'stack<...>::pop()' also returns 'void'. The
  20. > reason is simple: If you need the value, you can get it with a sequence
  21. > of 'top()' and 'pop()' operations. Requiring that 'pop()' returns an
  22. > object would make it necessary to create this object which in turn can
  23. > be expensive. This way it is defined everything is possible
  24. > efficiently.
  25.  
  26. Actually, I can think of another reason. Assuming the stack is defined as a
  27. robust throw-and-keep class - meaning its member functions leave the stack
  28. in its old state when they report an exception - then having a pop() that
  29. both returns the top element and discards it may still lead to trouble for
  30. its users. If we write:
  31.  
  32.     try {
  33.         someObj = someStack.pop();
  34.     }
  35.     catch (..) {
  36.         // state of someStack??? (*)
  37.     }
  38.  
  39. then, when we get to (*), we don't know what happened. Did the exception
  40. come from someStack.pop(), so the stack is still in its old state, or did
  41. it come from the assignment to someObj, so the stack is in its new state
  42. and the top element is lost? Recovering will be nearly impossible, so the
  43. stack should probably be shut down.
  44.  
  45. If we have a separate member function to get the top element (`T stack<T>
  46. top() const') and one to discard it (`void stack<T>::pop() throw()'), users
  47. can write:
  48.  
  49.     try {
  50.         someObj = someStack.top();
  51.     }
  52.     catch (..) {
  53.         // no harm done: report the error
  54.         return;
  55.     }
  56.  
  57.     // top element now safely stored in someObj:
  58.     // remove it from the stack
  59.     someStack.pop();
  60.  
  61. From this, I conclude that non-const member functions in a robust
  62. throw-and-keep class should only be allowed to return objects of a type T
  63. if both's T's copy constructor and assignment operator are known not to
  64. throw any exceptions. For more on throw-and-keep resources, see Harald
  65. Mueller's article in the January 1996 issue of the C++ Report.
  66.  
  67. - Wil
  68.  
  69. Wil Evers, <wil@ittpub.nl>
  70. ITT Publitec Research and Development BV, Amsterdam, Holland
  71.  
  72.  
  73. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your
  74.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  75.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  76.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  77.   Comments? mailto:std-c++-request@ncar.ucar.edu
  78. ]
  79.